﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Text.Json;
using SPUtils;
using StackExchange.Redis;

namespace RedisUtils
{
    public class Conf
    {
        public string Redis { get; set; }
    }
    

    public partial class Redis
    {
        private Dictionary<string, int> m_dicAliasID = new Dictionary<string, int>();
        private List<int> m_lstCompID = new List<int>();
        
        private SqlSugar.ISqlSugarClient m_xCn = null;
        private IDatabase m_xRedisDB = null;

        private List<Enums.PurePropDataType> m_lstPureCompProp = new List<Enums.PurePropDataType>();
        private List<Enums.PureTDepPropDataType> m_lstPureCompPropTDep = new List<Enums.PureTDepPropDataType>();
        private List<Enums.PropertyMethod> m_lstBinCompProp = new List<Enums.PropertyMethod>();

        public bool bUseJson = false;


        private enum PropCat
        {
            PureConstProp = 1,
            PureTDepProp = 2,
            Binary = 3
        }

        // MAIN
        public string ReadConf(string sPathConf, out Conf xConf)
        {
            string sErr = "";
            string sConfJson;

            xConf = new Conf();

            try
            {
                Console.WriteLine(" Read config " + sPathConf);

                sConfJson = File.ReadAllText(sPathConf);
                var options = new JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = true
                };

                xConf = JsonSerializer.Deserialize<Conf>(sConfJson, options);                
            }
            catch (Exception ex)
            {
                sErr = ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }
            return sErr;

        }

        public string DB2Redis_AliasID(IDatabase xRedisDB, SqlSugar.ISqlSugarClient cn)
        {
            string sErr = "";
            
            try
            {
                m_xCn = cn;
                m_xRedisDB = xRedisDB;

                sErr = ReadDB_Alias_PCdataID(m_xCn);
                if (sErr != "") goto Get_Out;

                sErr = SaveRedis_AliasID(m_dicAliasID, xRedisDB);
                if (sErr != "") goto Get_Out;
            }
            catch (Exception ex)
            {
                sErr = ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }
            return sErr;

        }

        public string DB2Redis_Props()
        {
            string sErr = "";

            Dictionary<int, string> dicIDPropJson;
            Dictionary<string, string> dicIDPropJson_bin;

            try
            {
                SPUtils.RedisUtils.InitPropLists(out m_lstPureCompProp, out m_lstPureCompPropTDep, out m_lstBinCompProp);

                // Pure Comp Const Props
                sErr = DBRead_PureCompProps(m_xCn, m_lstCompID, out dicIDPropJson);
                if (sErr != "") goto Get_Out;

                sErr = SaveRedis_Props(dicIDPropJson, PropCat.PureConstProp, "", m_xRedisDB);
                if (sErr != "") goto Get_Out;


                // Pure Comp TDep Props
                foreach (Enums.PureTDepPropDataType xPropType in m_lstPureCompPropTDep)
                {
                    sErr = DBRead_TDepProps(m_xCn, m_lstCompID, xPropType, out dicIDPropJson);
                    if (sErr != "") goto Get_Out;

                    sErr = SaveRedis_Props(dicIDPropJson, PropCat.PureTDepProp, xPropType.ToString(), m_xRedisDB);
                    if (sErr != "") goto Get_Out;
                }


                // Binary Comp Int Props 
                foreach (Enums.PropertyMethod xPropType in m_lstBinCompProp)
                {
                    sErr = DBRead_BinProps(m_xCn, m_lstCompID, xPropType, out dicIDPropJson_bin);
                    if (sErr != "") goto Get_Out;

                    sErr = SaveRedis_Props(dicIDPropJson_bin, PropCat.Binary, xPropType.ToString(), m_xRedisDB);
                    if (sErr != "") goto Get_Out;
                }

                m_xCn.Close();


            }
            catch (Exception ex)
            {
                sErr = ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }
            return sErr;

        }




    }
}
